package DBWrapper::HostsChecker;

# $Id: HostsChecker.pm 217 2010-07-12 18:57:17Z dolbanavt $
# $Date: 2010-07-12 21:57:29 +0300 (Пнд, 12 Июл 2010) $
# $HeadURL: https://robosearch.googlecode.com/svn/branches/reborn/lib/DBWrapper/HostsChecker.pm $
# $LastChangedRevision: 348 $

use parent qw( DBWrapper );
use JIP::ToolSet;
use Carp qw( croak );
use English qw( -no_match_vars );
use JIP::Utils qw( check_named_params );

our $VERSION = '1.0';

sub get_known_hosts {
    my $self = shift;

    my $statement = $self->get_dbh->prepare( q{SELECT ip_address FROM hosts} )
        or croak 'cannot prepare: '. $self->get_dbh->errstr;

    $statement->execute()
        or croak 'cannot execute: '. $self->get_dbh->errstr;

    my @hosts;

    while ( my $ln = $statement->fetchrow_arrayref() ) {
        push @hosts, $ln->[0];
    }

    return @hosts;
}

sub add_new_host {
    my ( $self, $ip_address ) = @ARG;

    croak 'ip_address required' unless defined $ip_address;

    $self->get_dbh->do(
        q{INSERT INTO hosts ( ip_address ) VALUES ( ? )},
        {},
        $ip_address,
    ) or croak 'cannot insert data: '. $self->get_dbh->errstr;

    return 1;
}

sub update_status_for {
    my ( $self, $ip_address ) = @ARG;

    croak 'ip_address required' unless defined $ip_address;

    $self->get_dbh->do(
        q{
            INSERT INTO network_scans ( host_id, last_scan )
            SELECT
                hosts.id, NOW()
            FROM
                hosts
            WHERE
                hosts.ip_address = ?
        },
        {},
        $ip_address,
    ) or croak 'cannot insert data: '. $self->get_dbh->errstr;

    return 1;
}

sub update_netbios_name {
    my $self  = shift;
    my %param = @ARG;

    check_named_params(
        rules_ref  => {
            ip_address   => { required => 1, defined  => 1 },
            netbios_name => { required => 1, defined  => 1 },
        },
        params_ref => \%param,
    );

    $self->get_dbh->do(
        q{UPDATE hosts SET netbios_name = ? WHERE ip_address = ?},
        {},
        $param{'netbios_name'},
        $param{'ip_address'},
    ) or croak 'cannot update data: '. $self->get_dbh->errstr;

    return 1;
}

1;

__END__

=head1 NAME

DBWrapper::HostsChecker - обертка над базой данных для хостчекера

=head1 SYNOPSIS

    use DBWrapper::HostsChecker;

    my $w = DBWrapper::HostsChecker->new( dbh => DBI->new() );

=head1 DEPENDENCIES

JIP::ToolSet (from base_lib)

JIP::Utils (from base_lib)

=head1 AUTHOR

Vladimir Zhavoronkov, <dolbanavt (at) Google Mail>

http://dolbanavt.livejournal.com/

=head1 LICENSE AND COPYRIGHT

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=cut
